<template>
  <div id="userMessage">
    <div class="userMaterialTitle">
      <h2 class="active">我的消息 <span></span></h2>
    </div>
    <div class="userMessage">
      <ul>
        <li v-for="(item, index) in myMessageData" :key="index">
          <h2><!-- <i></i> -->{{item.senderName}}: {{item.messageContent}}  <span>{{item.createTime}}</span> <span v-show="item.isMail === 1" @click="handleMessage(index)" class="userMessageContent">回复</span></h2>
          <!-- <p>content</p> -->
          <!-- 蒙层 -->
          <div class="message" v-show="isMessage">
            <div class="messageBox">
              <div class="messageTitle">站内信</div>
              <div>
                <textarea v-model="value" cols="30" rows="10"></textarea>
              </div>
              <span class="messageBtn sureBtn" @click="sureMessage()">确认</span>
              <span class="messageBtn" @click="handleMessage()">取消</span>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import store from '@/store'
export default {
  name: 'userMessage',
  data () {
    return {
      myMessageData: [],
      isMessage: false,
      value: '',
      index: ''
    }
  },
  created () {
    this.fetchMessage()
  },
  methods: {
    // 发私信
    sureMessage () {
      console.log(this.index)
      let userId = store.state.user.userId
      let senderId = this.myMessageData[this.index].sender
      if (this.userId == '') {
        this.$vux.toast.text('请先登录')
        return
      }
      console.log(senderId)
      if (this.value == '') {
        this.$vux.toast.text('内容不能为空')
        return
      }
      this.$api.PostPrivateLetter({
        messageContent: this.value,
        sender: userId,
        receiver: senderId
      }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.$vux.toast.text('回复成功')
          setTimeout(() => {
            this.handleMessage()
            this.value = ''
          }, 500)
        }
      })
    },
    // 显示/隐藏蒙层
    handleMessage (index) {
      this.isMessage = !this.isMessage
      this.index = index
    },
    // 获取我的消息列表
    fetchMessage () {
      this.$api.getMessage({
        userId: this.$store.state.user.userId,
        pageNum: 1
      }).then(res => {
        if (res.code === 0) {
          console.log(res)
          this.myMessageData = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
#userMessage {
  .message{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    padding-top: 200px;
    .messageBox{
      width: 600px;
      height: 400px;
      border-radius: 10px;
      background-color: #fff;
      position: absolute;
      // top: 200px;
      left: 0;
      right: 0;
      margin: 0 auto;
      text-align: center;
      .messageTitle{
        height: 60px;
        border-radius: 10px 10px 0 0;
        color: #fff;
        line-height: 60px;
        background-color: #00918A;
        // left: 50px;
      }
      .messageBtn{
        display: inline-block;
        width: 100px;
        height: 40px;
        border: 1px solid #aaaaaa;
        border-radius: 10px;
        line-height: 40px;
        margin-top: 30px;
      }
      .sureBtn{
        background-color: #00918A;
        color: #fff;
        margin-right: 50px;
      }
      div{
        textarea{
          margin-top: 20px;
          border: 1px solid #b4b4b4;
          border-radius: 10px;
          width: 80%;
          padding: 10px;
        }
      }
    }
  }
  .userMessage{
    ul{
      padding: .1rem .2rem;
      li:last-child {
        border: none;
      }
      li{
        padding: .1rem 0;
        border-bottom: 1px solid #eee;
        h2{
          font-size: .16rem;
          line-height: .24rem;
          font-size: 500;
          color: #333;
          padding-left: .3rem;
          position: relative;
          .userMessageContent{
            display: inline-block;
            width: 100px;
            height: 30px;
            border: 1px solid #aaaaaa;
            border-radius: 5px;
            text-align: center;
            line-height: 30x;
            margin-right: 50px;
          }
          i{
            display: inline-block;
            width: .1rem;
            height: .1rem;
            font-style: normal;
            background-color: #15A29C;
            border-radius: 500px;
            position: absolute;
            left: .1rem;
            top: 50%;
            transform: translateY(-50%);
          }
          span{
            float: right;
          }
        }
        p{
          font-size: .16rem;
          line-height: .24rem;
          font-size: 500;
          color: #999;
        }
      }
    }
  }
}
</style>
